package fu.k4c.vTeam.Main;

import java.util.HashMap;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;

import com.google.gson.Gson;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.TextureView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import fu.k4c.vTeam.R;
import fu.k4c.vTeam.Login.EditInfoActivity;
import fu.k4c.vTeam.Login.LoginActivity;
import fu.k4c.vTeam.Object.Email;
import fu.k4c.vTeam.Object.Information;
import fu.k4c.vTeam.Search.ScanActivity;
import fu.k4c.vTeam.Search.SearchActivity;

public class MainActivity extends Activity {
	int CURRENT_TAG = 0;
	static final int TAG_GUEST = 0;
	static final int TAG_USER = 1;
	String status = "";
	String email = "";
	String fullName = "";
	ProgressDialog proDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		overridePendingTransition(android.R.anim.fade_in,
				android.R.anim.fade_out);
		Bundle data = getIntent().getExtras();

		if (data != null) {
			email = data.getString("email");
			status = data.getString("status");
			fullName = data.getString("fullName");
		}

		final Button btn_main_info = (Button) findViewById(R.id.btn_main_info);
		final Button btn_main_back = (Button) findViewById(R.id.btn_main_logout);
		final TextView  tv_welcome = (TextView)findViewById(R.id.tv_main_welcome);
		if (fullName.equals("") || fullName == null){
			tv_welcome.setText("khách!");
		}else {
			tv_welcome.setText(fullName + "!");
		}
		if (status.equals("True")) {
			CURRENT_TAG = TAG_USER;
		} else {
			CURRENT_TAG = TAG_GUEST;
			btn_main_back.setText("Quay lại");
			btn_main_info.setText("Đăng nhập");
		}

		btn_main_info.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (CURRENT_TAG == TAG_GUEST) {
					Intent i = new Intent(MainActivity.this,
							LoginActivity.class);
					startActivity(i);
				} else {
					try {

						// Toast.makeText(getApplicationContext(),et_user.getText().toString()+","+et_pass.getText().toString()
						// , Toast.LENGTH_LONG).show();
						proDialog = ProgressDialog.show(MainActivity.this, "",
								"Đang kết nối đến máy chủ...");
						new Thread() {
							public void run() {

								try {
									sleep(1000);

								} catch (Exception e) {
									System.out.print(e.toString());
								}
								// dismiss the progressdialog
								proDialog.dismiss();
							}
						}.start();
						SchemeRegistry registry = new SchemeRegistry();
						registry.register(new Scheme("http", PlainSocketFactory
								.getSocketFactory(), 80));
						HttpParams httpParams = new BasicHttpParams();
						ThreadSafeClientConnManager connMgr = new ThreadSafeClientConnManager(
								httpParams, registry);
						HttpClient client = new DefaultHttpClient(connMgr,
								httpParams);
						HttpPost post = new HttpPost(
								getString(R.string.connectionString)
										+ "vLocationService/GetPersonalInformation");
						post.setHeader("Accept", "application/json");
						post.setHeader("Content-type", "application/json");
						HashMap<String, Object> postParameters = new HashMap<String, Object>();
						Email user = new Email(email);
						postParameters.put("UserName", user.getUsername());
						Gson gson = new Gson();
						String jsonParameters = gson.toJson(postParameters);
						// Toast.makeText(getApplicationContext(),jsonParameters,
						// Toast.LENGTH_LONG).show();
						post.setEntity(new StringEntity(jsonParameters));
						HttpResponse response = client.execute(post);

						HttpEntity entity = response.getEntity();
						String jsonresult = EntityUtils.toString(entity);
						// Toast.makeText(getApplicationContext(),jsonresult,
						// Toast.LENGTH_LONG).show();
						Information info = gson.fromJson(jsonresult,
								Information.class);
						Intent i = new Intent(MainActivity.this,
								EditInfoActivity.class);
						i.putExtra("email", user.getUsername());
						if (info.getAddress() != null) {
							i.putExtra("Address", info.getAddress());
						}
						if (info.getBirthday() != null) {
							i.putExtra("Birthday", info.getBirthday());
						}
						if (info.getPhone() != null) {
							i.putExtra("Phone", info.getPhone());
						}
						if (info.getSex() != null) {
							i.putExtra("Sex", info.getSex());
						}
						if (info.getFullName() != null) {
							i.putExtra("FullName", info.getFullName());
						}
						if (info.getRoleUser() != null) {
							i.putExtra("RoleUser", info.getRoleUser());
						}
						startActivity(i);

					} catch (Exception e) {
						// TODO: handle exception
						Toast.makeText(getApplicationContext(),
								"Bạn vui lòng kiểm tra lại kết nối Internet",
								Toast.LENGTH_SHORT).show();
					}

				}
			}
		});
		btn_main_back.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (CURRENT_TAG == TAG_GUEST) {
					finish();
				} else {
					new AlertDialog.Builder(MainActivity.this)
							.setMessage("Bạn muốn đăng xuất?")
							.setPositiveButton("Có",
									new DialogInterface.OnClickListener() {
										public void onClick(
												DialogInterface dialog,
												int whichButton) {
											CURRENT_TAG = TAG_GUEST;
											email = "";
											status = "";
											btn_main_back.setText("Quay lại");
											btn_main_info.setText("Đăng nhập");
											tv_welcome.setText("khách!");
											Toast.makeText(
													getApplicationContext(),
													"Bạn đã đăng xuất",
													Toast.LENGTH_LONG).show();
										}
									})
							.setNegativeButton("Không",
									new DialogInterface.OnClickListener() {
										public void onClick(
												DialogInterface dialog,
												int whichButton) {
										}
									}).create().show();
				}
			}
		});

		final ImageView imv_food = (ImageView) findViewById(R.id.subapp_dinner);
		ImageView imv_cafe = (ImageView) findViewById(R.id.subapp_coffee);
		ImageView imv_atm = (ImageView) findViewById(R.id.subapp_ATM);
		ImageView imv_entertaiment = (ImageView) findViewById(R.id.subapp_entertaiment);
		ImageView imv_hopital = (ImageView) findViewById(R.id.subapp_hospital);
		ImageView imv_petrol = (ImageView) findViewById(R.id.subapp_petrol);
		ImageView imv_shop = (ImageView) findViewById(R.id.subapp_shop);
		ImageView imv_event = (ImageView) findViewById(R.id.subapp_event);
		ImageView imv_search = (ImageView) findViewById(R.id.subapp_search);
		ImageView imv_scan = (ImageView) findViewById(R.id.subapp_suggest);
		imv_scan.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				proDialog = ProgressDialog.show(MainActivity.this, "",
						"Đang kết nối đến máy chủ...");
				new Thread() {
					public void run() {

						try {
							sleep(500);

						} catch (Exception e) {
							System.out.print(e.toString());
						}
						// dismiss the progressdialog
						proDialog.dismiss();
					}
				}.start();
				if (CURRENT_TAG == TAG_GUEST) {
					Toast.makeText(getApplicationContext(),
							"Đăng nhập để sử dụng chức năng này!",
							Toast.LENGTH_LONG).show();
				} else {
					Intent i = new Intent(MainActivity.this, ScanActivity.class);
					i.putExtra("status", status);
					i.putExtra("email", email);
					startActivity(i);
				}
			}
		});
		imv_search.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				proDialog = ProgressDialog.show(MainActivity.this, "",
						"Đang kết nối đến máy chủ...");
				new Thread() {
					public void run() {

						try {
							sleep(500);

						} catch (Exception e) {
							System.out.print(e.toString());
						}
						// dismiss the progressdialog
						proDialog.dismiss();
					}
				}.start();
				Intent i = new Intent(MainActivity.this, SearchActivity.class);
				i.putExtra("status", status);
				i.putExtra("email", email);
				startActivity(i);
			}
		});
		imv_food.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				proDialog = ProgressDialog.show(MainActivity.this, "",
						"Đang kết nối đến máy chủ...");
				new Thread() {
					public void run() {

						try {
							sleep(500);

						} catch (Exception e) {
							System.out.print(e.toString());
						}
						// dismiss the progressdialog
						proDialog.dismiss();
					}
				}.start();
				Intent i = new Intent(MainActivity.this,
						ViewPlaceActivity.class);
				i.putExtra("CategorieName", "amthuc");
				i.putExtra("Category", "Ẩm thực");
				i.putExtra("status", status);
				i.putExtra("email", email);
				startActivity(i);

			}
		});
		imv_atm.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				proDialog = ProgressDialog.show(MainActivity.this, "",
						"Đang kết nối đến máy chủ...");
				new Thread() {
					public void run() {

						try {
							sleep(500);

						} catch (Exception e) {
							System.out.print(e.toString());
						}
						// dismiss the progressdialog
						proDialog.dismiss();
					}
				}.start();
				Intent i = new Intent(MainActivity.this,
						ViewPlaceActivity.class);
				i.putExtra("CategorieName", "atm");
				i.putExtra("Category", "ATM");
				i.putExtra("status", status);
				i.putExtra("email", email);
				startActivity(i);

			}
		});
		imv_cafe.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				proDialog = ProgressDialog.show(MainActivity.this, "",
						"Đang kết nối đến máy chủ...");
				new Thread() {
					public void run() {

						try {
							sleep(500);

						} catch (Exception e) {
							System.out.print(e.toString());
						}
						// dismiss the progressdialog
						proDialog.dismiss();
					}
				}.start();
				Intent i = new Intent(MainActivity.this,
						ViewPlaceActivity.class);
				i.putExtra("CategorieName", "cafe");
				i.putExtra("Category", "Cà phê");
				i.putExtra("status", status);
				i.putExtra("email", email);
				startActivity(i);
			}
		});
		imv_entertaiment.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				proDialog = ProgressDialog.show(MainActivity.this, "",
						"Đang kết nối đến máy chủ...");
				new Thread() {
					public void run() {

						try {
							sleep(500);

						} catch (Exception e) {
							System.out.print(e.toString());
						}
						// dismiss the progressdialog
						proDialog.dismiss();
					}
				}.start();
				Intent i = new Intent(MainActivity.this,
						ViewPlaceActivity.class);
				i.putExtra("CategorieName", "giaitri");
				i.putExtra("Category", "Giải trí");
				i.putExtra("status", status);
				i.putExtra("email", email);
				startActivity(i);
			}
		});
		imv_event.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				proDialog = ProgressDialog.show(MainActivity.this, "",
						"Đang kết nối đến máy chủ...");
				new Thread() {
					public void run() {

						try {
							sleep(500);

						} catch (Exception e) {
							System.out.print(e.toString());
						}
						// dismiss the progressdialog
						proDialog.dismiss();
					}
				}.start();
				Intent i = new Intent(MainActivity.this,
						ViewEventActivity.class);
				i.putExtra("status", status);
				i.putExtra("email", email);
				startActivity(i);
			}
		});
		imv_hopital.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				proDialog = ProgressDialog.show(MainActivity.this, "",
						"Đang kết nối đến máy chủ...");
				new Thread() {
					public void run() {

						try {
							sleep(500);

						} catch (Exception e) {
							System.out.print(e.toString());
						}
						// dismiss the progressdialog
						proDialog.dismiss();
					}
				}.start();
				Intent i = new Intent(MainActivity.this,
						ViewPlaceActivity.class);
				i.putExtra("CategorieName", "benhvien");
				i.putExtra("Category", "Bệnh viện");
				i.putExtra("status", status);
				i.putExtra("email", email);
				startActivity(i);
			}
		});
		imv_petrol.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				proDialog = ProgressDialog.show(MainActivity.this, "",
						"Đang kết nối đến máy chủ...");
				new Thread() {
					public void run() {

						try {
							sleep(500);

						} catch (Exception e) {
							System.out.print(e.toString());
						}
						// dismiss the progressdialog
						proDialog.dismiss();
					}
				}.start();
				Intent i = new Intent(MainActivity.this,
						ViewPlaceActivity.class);
				i.putExtra("CategorieName", "tramxang");
				i.putExtra("Category", "Trạm xăng");
				i.putExtra("status", status);
				i.putExtra("email", email);
				startActivity(i);
			}
		});
		imv_shop.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				proDialog = ProgressDialog.show(MainActivity.this, "",
						"Đang kết nối đến máy chủ...");
				new Thread() {
					public void run() {

						try {
							sleep(500);

						} catch (Exception e) {
							System.out.print(e.toString());
						}
						// dismiss the progressdialog
						proDialog.dismiss();
					}
				}.start();
				Intent i = new Intent(MainActivity.this,
						ViewPlaceActivity.class);
				i.putExtra("CategorieName", "shopping");
				i.putExtra("Category", "Mua sắm");
				i.putExtra("status", status);
				i.putExtra("email", email);
				startActivity(i);
			}
		});
	}

}
